home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
By the Book
/
Mac C Primer V2
/
4.3 - ColorTutor
/
ColorTutor.c
next >
Wrap
C/C++ Source or Header
|
1991-08-21
|
12KB
|
590 lines
/************************************************************/
/* */
/* ColorTutor Code from Chapter Four of */
/* */
/* *** The Macintosh Programming Primer *** */
/* */
/* Copyright 1990, Dave Mark */
/* */
/* This program demonstrates specific Mac programming */
/* techniques. */
/* */
/************************************************************/
#include "Palettes.h"
#include "Picker.h"
#define BASE_RES_ID 400
#define NIL_POINTER 0L
#define NIL_STRING "\p"
#define VISIBLE TRUE
#define HAS_GOAWAY TRUE
#define MOVE_TO_FRONT (WindowPtr)-1L
#define REMOVE_ALL_EVENTS 0
#define MIN_SLEEP 0L
#define NIL_MOUSE_REGION 0L
#define NOT_A_NORMAL_MENU -1
#define PRECISE_TOLERANCE 0x0000
#define BLACK_PATTERN 1
#define GRAY_PATTERN 2
#define COLOR_RAMP 4
#define GRAY_RAMP 5
#define SINGLE_COLOR 6
#define SRC_AND_BACK_MENU 400
#define MODE_MENU 401
Boolean IsColor(), PickColor();
Rect gSrcRect, gBackRect, gDestRect,
gSrcMenuRect, gBackMenuRect, gModeMenuRect,
gOpColorRect;
int gSrcPattern, gBackPattern, gCopyMode,
gSrcType, gBackType;
RGBColor gSrcColor, gBackColor, gOpColor;
MenuHandle gSrcMenu, gBackMenu, gModeMenu;
WindowPtr gColorWindow;
main()
{
Point corner;
PaletteHandle pal;
ToolBoxInit();
if ( ! IsColor() )
DoAlert( "\pThis machine does not support Color QuickDraw!" );
else
{
SetUpWindow();
SetUpGlobals();
DoEventLoop();
}
}
/*********************************** ToolBoxInit */
ToolBoxInit()
{
InitGraf( &thePort );
InitFonts();
FlushEvents( everyEvent, REMOVE_ALL_EVENTS );
InitWindows();
InitMenus();
TEInit();
InitDialogs( NIL_POINTER );
InitCursor();
}
/*********************************** SetUpWindow */
SetUpWindow()
{
Rect r;
SetRect( &r, 5, 40, 225, 275 );
gColorWindow = NewCWindow( NIL_POINTER, &r, "\pColorTutor",
VISIBLE, noGrowDocProc, MOVE_TO_FRONT,
HAS_GOAWAY, NIL_POINTER );
SetRect( &r, 15, 207, 95, 225 );
NewControl( gColorWindow, &r, "\pOpColor...",
VISIBLE, 0, 0, 1, pushButProc, NIL_POINTER );
SetPort( gColorWindow );
TextFont( systemFont );
}
/*********************************** SetUpGlobals */
SetUpGlobals()
{
SetRect( &gSrcRect, 15, 6, 95, 86 );
SetRect( &gBackRect, 125, 6, 205, 86 );
SetRect( &gDestRect, 125, 122, 205, 202 );
SetRect( &gOpColorRect, 15, 122, 95, 202 );
SetRect( &gSrcMenuRect, 7, 90, 103, 108 );
SetRect( &gBackMenuRect, 117, 90, 213, 108 );
SetRect( &gModeMenuRect, 117, 206, 213, 226 );
gSrcPattern = BLACK_PATTERN;
gBackPattern = BLACK_PATTERN;
gCopyMode = srcCopy;
gSrcColor.red = 65535;
gSrcColor.green = gSrcColor.blue = 0;
gSrcType = SINGLE_COLOR;
gBackColor.blue = 0xFFFF;
gBackColor.red = gBackColor.green = 0;
gBackType = SINGLE_COLOR;
gOpColor.green = 32767;
gOpColor.red = 32767;
gOpColor.blue = 32767;
OpColor( &gOpColor );
gSrcMenu = GetMenu( SRC_AND_BACK_MENU );
InsertMenu( gSrcMenu, NOT_A_NORMAL_MENU );
gBackMenu = GetMenu( SRC_AND_BACK_MENU );
InsertMenu( gBackMenu, NOT_A_NORMAL_MENU );
gModeMenu = GetMenu( MODE_MENU );
InsertMenu( gModeMenu, NOT_A_NORMAL_MENU );
}
/*********************************** DoEventLoop */
DoEventLoop()
{
Boolean done;
EventRecord e;
short part;
WindowPtr window;
Point p;
done = FALSE;
while ( ! done )
{
WaitNextEvent( everyEvent, &e, MIN_SLEEP, NIL_MOUSE_REGION );
switch( e.what )
{
case mouseDown:
part = FindWindow( e.where, &window );
if ( part == inGoAway )
done = TRUE;
else if ( part == inDrag )
DragWindow( window, e.where, &screenBits.bounds );
else if ( part == inContent )
{
p = e.where;
GlobalToLocal( &p );
DoContent( p );
}
break;
case updateEvt:
BeginUpdate( (WindowPtr)e.message );
SetPort( (WindowPtr)e.message );
DrawWindow();
DrawControls( (WindowPtr)e.message );
EndUpdate( (WindowPtr)e.message );
break;
}
}
}
/*********************************** DoContent */
DoContent( p )
Point p;
{
int choice;
ControlHandle control;
RGBColor rgbColor;
if ( FindControl( p, gColorWindow, &control ) )
{
if ( TrackControl( control, p, NIL_POINTER ) )
{
rgbColor = gOpColor;
if ( PickColor( &rgbColor ) )
{
gOpColor = rgbColor;
InvalRect( &gOpColorRect );
InvalRect( &gDestRect );
OpColor( &gOpColor );
}
}
}
else if ( PtInRect( p, &gSrcMenuRect ) )
{
UpdateSrcMenu();
choice = DoPopup( gSrcMenu, &gSrcMenuRect );
if ( choice > 0 )
{
DoSrcChoice( choice );
InvalRect( &gSrcRect );
InvalRect( &gDestRect );
}
}
else if ( PtInRect( p, &gBackMenuRect ) )
{
UpdateBackMenu();
choice = DoPopup( gBackMenu, &gBackMenuRect );
if ( choice > 0 )
{
DoBackChoice( choice );
InvalRect( &gBackRect );
InvalRect( &gDestRect );
}
}
else if ( PtInRect( p, &gModeMenuRect ) )
{
UpdateModeMenu();
choice = DoPopup( gModeMenu, &gModeMenuRect );
if ( choice > 0 )
{
DoModeChoice( choice );
InvalRect( &gDestRect );
}
}
}
/*********************************** DrawWindow */
DrawWindow()
{
RGBColor rgbBlack;
Rect source, dest;
rgbBlack.red = rgbBlack.green = rgbBlack.blue = 0;
if ( gSrcPattern == BLACK_PATTERN )
PenPat( black );
else
PenPat( gray );
if ( gSrcType == COLOR_RAMP )
DrawColorRamp( &gSrcRect );
else if ( gSrcType == GRAY_RAMP )
DrawGrayRamp( &gSrcRect );
else
{
RGBForeColor( &gSrcColor );
PaintRect( &gSrcRect );
}
if ( gBackPattern == BLACK_PATTERN )
PenPat( black );
else
PenPat( gray );
if ( gBackType == COLOR_RAMP )
DrawColorRamp( &gBackRect );
else if ( gBackType == GRAY_RAMP )
DrawGrayRamp( &gBackRect );
else
{
RGBForeColor( &gBackColor );
PaintRect( &gBackRect );
}
PenPat( black );
RGBForeColor( &gOpColor );
PaintRect( &gOpColorRect );
RGBForeColor( &rgbBlack );
DrawLabel( &gSrcMenuRect, "\pSource" );
DrawLabel( &gBackMenuRect, "\pBackground" );
DrawLabel( &gModeMenuRect, "\pMode" );
PenSize( 2, 2 );
FrameRect( &gSrcRect );
FrameRect( &gBackRect );
FrameRect( &gDestRect );
FrameRect( &gOpColorRect );
PenNormal();
source = gBackRect;
InsetRect( &source, 2, 2 );
dest = gDestRect;
InsetRect( &dest, 2, 2 );
CopyBits( &((CGrafPtr)gColorWindow)->portPixMap,
&((CGrafPtr)gColorWindow)->portPixMap,
&source, &dest, srcCopy, NIL_POINTER );
source = gSrcRect;
InsetRect( &source, 2, 2 );
CopyBits( &((CGrafPtr)gColorWindow)->portPixMap,
&((CGrafPtr)gColorWindow)->portPixMap,
&source, &dest, gCopyMode, NIL_POINTER );
}
/*********************************** DrawColorRamp */
DrawColorRamp( rPtr )
Rect *rPtr;
{
long numColors, i;
HSVColor hsvColor;
RGBColor rgbColor;
Rect r;
r = *rPtr;
InsetRect( &r, 2, 2 );
numColors = (rPtr->right - rPtr->left - 2) / 2;
hsvColor.value = hsvColor.saturation = 65535;
for ( i=0; i<numColors; i++ )
{
hsvColor.hue = i * 65535 / numColors;
HSV2RGB( &hsvColor, &rgbColor );
RGBForeColor( &rgbColor );
FrameRect( &r );
InsetRect( &r, 1, 1 );
}
}
/*********************************** DrawGrayRamp */
DrawGrayRamp( rPtr )
Rect *rPtr;
{
long numColors, i;
RGBColor rgbColor;
Rect r;
r = *rPtr;
InsetRect( &r, 2, 2 );
numColors = (rPtr->right - rPtr->left - 2) / 2;
for ( i=0; i<numColors; i++ )
{
rgbColor.red = i * 65535 / numColors;
rgbColor.green = rgbColor.red;
rgbColor.blue = rgbColor.red;
RGBForeColor( &rgbColor );
FrameRect( &r );
InsetRect( &r, 1, 1 );
}
}
/*********************************** DrawLabel */
DrawLabel( rPtr, s )
Rect *rPtr;
Str255 s;
{
Rect tempRect;
int size;
tempRect = *rPtr;
tempRect.bottom -= 1;
tempRect.right -= 1;
FrameRect( &tempRect );
MoveTo( tempRect.left + 1, tempRect.bottom );
LineTo( tempRect.right, tempRect.bottom );
LineTo( tempRect.right, tempRect.top + 1 );
size = rPtr->right - rPtr->left - StringWidth( s );
MoveTo( rPtr->left + size/2, rPtr->bottom - 6 );
DrawString( s );
}
/*********************************** UpdateSrcMenu */
UpdateSrcMenu()
{
int i;
for ( i=1; i<=6; i++ )
CheckItem( gSrcMenu, i, FALSE );
if ( gSrcPattern == BLACK_PATTERN )
CheckItem( gSrcMenu, BLACK_PATTERN, TRUE );
else
CheckItem( gSrcMenu, GRAY_PATTERN, TRUE );
if ( gSrcType == COLOR_RAMP )
CheckItem( gSrcMenu, COLOR_RAMP, TRUE );
else if ( gSrcType == GRAY_RAMP )
CheckItem( gSrcMenu, GRAY_RAMP, TRUE );
else if ( gSrcType == SINGLE_COLOR )
CheckItem( gSrcMenu, SINGLE_COLOR, TRUE );
}
/*********************************** UpdateBackMenu */
UpdateBackMenu()
{
int i;
for ( i=1; i<=6; i++ )
CheckItem( gBackMenu, i, FALSE );
if ( gBackPattern == BLACK_PATTERN )
CheckItem( gBackMenu, BLACK_PATTERN, TRUE );
else
CheckItem( gBackMenu, GRAY_PATTERN, TRUE );
if ( gBackType == COLOR_RAMP )
CheckItem( gBackMenu, COLOR_RAMP, TRUE );
else if ( gBackType == GRAY_RAMP )
CheckItem( gBackMenu, GRAY_RAMP, TRUE );
else if ( gBackType == SINGLE_COLOR )
CheckItem( gBackMenu, SINGLE_COLOR, TRUE );
}
/*********************************** UpdateModeMenu */
UpdateModeMenu()
{
int i;
for ( i=1; i<=17; i++ )
CheckItem( gModeMenu, i, FALSE );
if ( ( gCopyMode >=0 ) && ( gCopyMode <= 7 ) )
CheckItem( gModeMenu, gCopyMode + 1, TRUE );
else
CheckItem( gModeMenu, gCopyMode - 22, TRUE );
}
/*********************************** DoSrcChoice */
DoSrcChoice( item )
int item;
{
RGBColor rgbColor;
switch( item )
{
case BLACK_PATTERN:
gSrcPattern = BLACK_PATTERN;
break;
case GRAY_PATTERN:
gSrcPattern = GRAY_PATTERN;
break;
case COLOR_RAMP:
gSrcType = COLOR_RAMP;
break;
case GRAY_RAMP:
gSrcType = GRAY_RAMP;
break;
case SINGLE_COLOR:
gSrcType = SINGLE_COLOR;
rgbColor = gSrcColor;
if ( PickColor( &rgbColor ) )
gSrcColor = rgbColor;
break;
}
}
/*********************************** DoBackChoice */
DoBackChoice( item )
int item;
{
RGBColor rgbColor;
switch( item )
{
case BLACK_PATTERN:
gBackPattern = BLACK_PATTERN;
break;
case GRAY_PATTERN:
gBackPattern = GRAY_PATTERN;
break;
case COLOR_RAMP:
gBackType = COLOR_RAMP;
break;
case GRAY_RAMP:
gBackType = GRAY_RAMP;
break;
case SINGLE_COLOR:
gBackType = SINGLE_COLOR;
rgbColor = gBackColor;
if ( PickColor( &rgbColor ) )
gBackColor = rgbColor;
break;
}
}
/*********************************** DoModeChoice */
DoModeChoice( item )
int item;
{
if ( ( item >= 1 ) && ( item <= 8 ) )
gCopyMode = item - 1;
else
gCopyMode = item + 22;
}
/******************************** DoPopup *******/
int DoPopup( menu, rPtr )
MenuHandle menu;
Rect *rPtr;
{
Point corner;
long theChoice = 0L;
corner.h = rPtr->left;
corner.v = rPtr->bottom;
LocalToGlobal( &corner );
InvertRect( rPtr );
theChoice = PopUpMenuSelect( menu, corner.v - 1, corner.h + 1, 0 );
InvertRect( rPtr );
return( LoWord( theChoice ) );
}
/******************************** PickColor *********/
Boolean PickColor( colorPtr )
RGBColor *colorPtr;
{
Point where;
where.h = -1;
where.v = -1;
return( GetColor( where, "\pChoose a color...", colorPtr, colorPtr ) );
}
/******************************** IsColor *********/
Boolean IsColor()
{
SysEnvRec mySE;
SysEnvirons( 1, &mySE );
return( mySE.hasColorQD );
}
/*********************************** DoAlert */
DoAlert( s )
Str255 s;
{
ParamText( s, NIL_STRING, NIL_STRING, NIL_STRING );
NoteAlert( BASE_RES_ID, NIL_POINTER );
}